#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.direname(File.dirname(File.realpath($PROGRAM_NAME)))
require "#{LKP_SRC}/lib/tests/stats"
stats = LKP::Stats.new

if ENV['RESULT_ROOT']
    fn = File.join ENV['RESULT_ROOT'], 'output'
elsif ARGV[0]
    fn = ARGV[0]
end

contents = File.read(fn).split("\n")

cur_case = nil

contents.each do |line|
    case line
    # Sat Jun 10 14:45:25 2023 - INFO   - start to run testcase:oe_test_cockpit
    when /^\w{3}\s+\w{3}\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\d{4}\s+-\s+(\w{4,7})\s+-\s+start to run testcase:(\w+).$/
        cur_case = $2
    
    # Sat Jun 10 14:45:25 2023 - INFO   - The case exit by code 0
    # Sat Jun 10 14:45:25 2023 - ERROR  - The case exit by code 2
    when /^\w{3}\s+\w{3}\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\d{4}\s+-\s+(\w{4,7})\s+-\s+The case exit by code (\d+).$/
        if $1 == "INFO" and $2 == "0" and cur_case != nil
            puts ".result.#{cur_case}: pass"
            stats.add_test_result line, cur_case, "pass"
        elsif cur_case != nil
            puts ".result.#{cur_case}: failed"
            stats.add_test_result line, cur_case, "fail"
        end

    # Sat Jun 10 14:45:25 2023 - INFO   - A total of 10 use cases were executed, with 7 successes and 3 failures.
    when /^\w{3}\s+\w{3}\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\d{4}\s+-\s+INFO\s+-\s+A total of (\d+) use cases were executed, with (\d+) successes and (\d+) failures.$/
      puts "ttotal.nr_tests: #{$1}"
      puts "ttotal.nr_pass: #{$2}"
      puts "ttotal.nr_fail: #{$3}"
    end
end

stats.dump
